package com.bp.exam.controller.supports;

import com.bianmaba.spring.data.jpa.domain.PageableRequest;
import com.bianmaba.spring.mvc.controller.ReadController;
import com.bianmaba.spring.mvc.method.support.annotations.EntityParam;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.bianmaba.spring.mvc.method.support.annotations.MapParam;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @program: bianmaba-web-framework-extends
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-23 20:05
 **/
public abstract class DefaultReadController<T, ID extends Serializable> extends ReadController<T, ID> {


    protected UserDetails getCurrentUser() {
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return (UserDetails) obj;
    }

    protected String getCurrentUsername() {
        UserDetails userDetails = getCurrentUser();
        return userDetails.getUsername();
    }


    @JSON()
    @ApiOperation(value = "根据数据ID查询单个数据", notes = "如果指定ID的数据不存在，则返回NULL")
    @ApiImplicitParam(name = "id", value = "查询", dataType = "int", paramType = "query")
    @RequestMapping(value = "/get_by_id", method = RequestMethod.POST)
    public T getById(ID id) {
        T data = getService().findOne(id);
        return data;
    }

    @JSON()
    @ApiOperation(value = "获取所有数据", notes = "返回数据中的data为一个数据列表")
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public List<T> all() {
        List<T> data = getService().all();
        return data;
    }

    @JSON()

    @ApiOperation(value = "综合查询接口", notes = "返回数据中的data为一个数据列表<br/>此函数有2组参数，第一组是查询参数，以params开头，第二组为分页参数，以pageable开头<br/>参数示例如下（支持form data或queryString），<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.username_eq:maomao(表示username等于maomao,eq表示等于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.age_gt:10(表示age大于10，gt表示大于，ge为大于等于，le为小于等于，lt为小于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.xxx_like:val%(表示用某个属性做LIKE比较,比较值为val%，另外支持notlike)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.page:0(当前页)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.start:0(起始记录)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.size:25(共取多少条数据)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.sort:【{\"property\":\"cdt\",\"direction\":\"ASC\"}】(用cdt降序排序，支持多组排序)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Page query(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        Page<T> page = getService().query(params, pageable);
        return page;
    }

    @JSON()
    @ApiOperation(value = "去重复的综合查询接口", notes = "返回数据中的data为一个数据列表<br/>此函数有2组参数，第一组是查询参数，以params开头，第二组为分页参数，以pageable开头<br/>参数示例如下（支持form data或queryString），<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.username_eq:maomao(表示username等于maomao,eq表示等于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.age_gt:10(表示age大于10，gt表示大于，ge为大于等于，le为小于等于，lt为小于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.xxx_like:val%(表示用某个属性做LIKE比较,比较值为val%，另外支持notlike)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.page:0(当前页)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.start:0(起始记录)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.size:25(共取多少条数据)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.sort:【{\"property\":\"cdt\",\"direction\":\"ASC\"}】(用cdt降序排序，支持多组排序)")
    @RequestMapping(value = "/query_distinct", method = RequestMethod.POST)
    public Page queryDistinct(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        Page<T> page = getService().queryDistinct(params, pageable);
        return page;
    }
}
